import config from '../../utils/config';
import request from '../../utils/request';

let isSend = false;

Page({
    data:{
        placeholderContent: '',
        hotList:[],
        searchContent:'',
        searchList:[]

    },
    onLoad: function(){
        this.getInitData();

    },
    async getInitData(){
        const res = await request(config.searchKeyWords);
        const hotRes = await request(config.searchHot);
        this.setData({
            placeholderContent: res.data.showKeyword,
            hotList: hotRes.data
        });
    },
    // 搜索框输入
    handleInputChange(e){
        if(!e.detail.value.trim()){
            this.setData({
                searchList:[]
            });
            return;
        }
        // 更新searchContent的状态数据
        this.setData({
            searchContent: e.detail.value.trim()
        });

        if(isSend){
            return;
        }
        isSend = true;
        
        // 函数节流
        setTimeout(async () => {
            // 发请求获取关键字模糊匹配数据
            const res = await request(config.search,{ keywords: this.data.searchContent,limit: 10 });
            this.setData({
                searchList: res.result.songs
            });
            isSend = false;
        }, 500);
    }
})